import java.io.*;
import java.util.*;

public class GenerateHtmlFrame {

    //private static RandomAccessFile outputFile;
    private static DataOutputStream outputFile;
    //private static RandomAccessFile outputFileJs;
    private static DataOutputStream outputFileJs;
    private static String dir, subDir;
    private static String[] fileList;
    private static String[] subFileList;
    private static File [] dirList;
    private static String myUrl = "http://www.yahoo.com";
    private static String urlName = "Yahoo";
    private static Hashtable table = new Hashtable ();

    public static void makeHtmlFrame( )
    {

	String[] file_list     = null;
	String[] sub_file_list = null;
	String last_dir_name   = null;
	File f_file_list       = null;
	GenerateHtmlFrame maker = new GenerateHtmlFrame();
	dir = "./../charts/";
	subDir = null;

	maker.createLeftBarFile();
	maker.loadLeftBarProfile();
	
	maker.addHtmlHeadToLeftBar();
	maker.generateJavaScriptFile(dir, "setCookie.js");
	//maker.addJavaScriptToLeftBar();
	maker.addUserProfileToLeftBar( );

	file_list = maker.getDirList(dir);

	maker.addDirListToLeftBar(dir, file_list, "*");

	for (int n=0; n <file_list.length; n++) {
	    f_file_list = new File (dir, file_list[n]);
	    
	    if (f_file_list.isDirectory()) {
		subDir = file_list[n];
		System.out.println("subDir="+subDir);

		if(!subDir.startsWith("Save")) {
		    maker.addMenuBarForList(subDir);
		    sub_file_list = maker.getDirList(dir+subDir+"/");
		    maker.addDirListToLeftBar((dir+subDir+"/"), sub_file_list,">");
		}
	    }
	    else {
		System.out.println("n="+file_list[n]);
	    }
	}
	maker.addHtmlEnd();
	maker.closeFile(outputFile);
    }

    public String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }

    private void generateJavaScriptFile(String jsdir, String jsfile)
    {

	File fileName = new File (jsdir, jsfile);
	try {
	    if (fileName.exists()) {
		System.out.println("deleting old "+jsfile+" file .....");
		if(fileName.delete()) {
		    System.out.println("Old "+jsfile+" file has been deleted");
		}
	    }
	    outputFileJs = new DataOutputStream(new FileOutputStream(fileName));
	    if ((fileName.getName()).equals("setCookie.js")) {
		outputFileJs.writeBytes("function setCurrState(setting) {");
		outputFileJs.writeBytes("document.cookie = \"currState=\" + escape(setting);");
		outputFileJs.writeBytes("currState = setting};");
		closeFile(outputFileJs);
	    }
	}
	catch ( IOException e ) {
	}
    }

    private void createLeftBarFile()
    {

	File fileName = new File (dir, "index_left.html");
	try {
	    if (fileName.exists()) {
		System.out.println("deleting old index_left.html file .....");
		if(fileName.delete()) {
		    System.out.println("Old index_left.html file has been deleted");
		}
	    }
	    outputFile = new DataOutputStream(new FileOutputStream(fileName));

	}
	catch ( IOException e ) {
	}
    }

    private void loadLeftBarProfile() {

	SymbolFileLoader loader = new SymbolFileLoader();
	loader.setFileName(dir+"profile.txt");
	loader.loadUrlProfile();
	table = loader.getHashTable();
	printHashTable(table);
    }


    public void addHtmlHeadToLeftBar( )
    {
       try {
	   outputFile.writeBytes("<html><head>" +
				 "<title>Watch List Left Bar</title>");
       }
       catch ( IOException io ) {
       }
    }

    private void addMenuBarForList(String sub_dir) {
	String current = sub_dir;
	if (current == null || current.startsWith("save") || current.startsWith("index")) {
	    return;
	}

	try {
	    outputFile.writeBytes("<TR><TD></TD></TR><TR><TD>"+current.toUpperCase()+"</TD></TR>");
	}
	catch ( IOException io ) {
	}
    }

    public void addUserProfileToLeftBar( )
    {
	Hashtable profile = new Hashtable ();

       try {
	
	   Enumeration enum = table.keys();  
	   while (enum.hasMoreElements()) {
	       urlName = (String)enum.nextElement();
	       myUrl = (String)table.get(urlName);
	       outputFile.writeBytes("<BODY><TABLE><TR><TD>*<A href=\""+myUrl);
	       outputFile.writeBytes("\" TARGET=\"mainFrame\"><font size='2'>&nbsp");
	       outputFile.writeBytes(urlName.toUpperCase());
	       outputFile.writeBytes("</font></A></tr></td>");
	   }
       }
       catch ( IOException io ) {
       }
    }


    public void addDirListToLeftBar(String dir, String[] file_list, String list )
    {

	File f_file_list;
	int i = 0;
	while (i < file_list.length) {
	    f_file_list = new File (dir, file_list[i]);

	    if (f_file_list.isFile()) {
		addFileToLeftBar(dir, file_list[i], list);
		i++;
	    }
	    else {
		i++;
		continue;
	    }
	}
    }

    public void addFileToLeftBar(String dir,  String file_list, String list )
    {

       try {

	   File f_file_list = new File (dir, file_list);

	   if (f_file_list.isFile()) {
	       if (!(f_file_list.getName().startsWith("index")) && !(f_file_list.getName().startsWith("profile"))) {
		
		   outputFile.writeBytes("<TR><TD><A HREF = 'http:\\\\localhost:8080\\stockapp\\servlet\\DeletFileServlet?id=");
		   outputFile.writeBytes(f_file_list.getAbsolutePath()+"'>"+
					 "<img border='0' src='.\\SaveImage\\x.gif' width='10' height='10'></A>"+
					 list+"<A HREF=\"");
		   outputFile.writeBytes(dir+f_file_list.getName());
		   outputFile.writeBytes("\" TARGET=\"mainFrame\"><font size='2'>"+
		       (f_file_list.getName().toUpperCase()).replaceAll(".HTML", "") + 
					 "</font></A></TD></TR>");
	       }
	   }
	   else {
	       System.out.println("dir="+dir+"  "+"file"+file_list+" Not found!");
	   }
       }
       catch ( IOException io ) {
       }
    }

    public void addHtmlEnd( )
    {
	try {
	    outputFile.writeBytes("</TABLE></body></html>");
	}
	catch ( IOException io ) {
	}
    }

    private void closeFile(DataOutputStream raf) 
    {
	try {
	    raf.close();
	    System.exit( 0 );
	}
	catch( IOException ex ) {
	    System.exit( 1 );
	}
    }

    public void printHashTable (Hashtable htb) {
	Enumeration enum = htb.keys();  
	while (enum.hasMoreElements()) {
	    System.out.println(enum.nextElement());
	}
    }

    public static void main( String args[] )
    {
	GenerateHtmlFrame maker = new GenerateHtmlFrame();
	maker.makeHtmlFrame();
    }
}



